<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2019/10/28
 * Time: 15:12
 */

namespace app\adminapiv3\controller\platform_manage\account;

//用户管理
use app\adminapiv3\controller\BaseController;
use app\adminapiv3\model\ServerHostGroupModel;
use app\adminapiv3\model\ServerUserModel;
use app\adminapiv3\service\platform_manage\account\ServerUserService;
use app\common\exception\CustomException;
use think\Request;

class ServerUserController extends BaseController
{
    /**
     * @name 列表
     * @author wx
     * @date 2019/10/28 15:22
     */
    public function list(Request $request)
    {
        //搜索参数
        $search = $request->only([
            'group_id','real_name','show_name','email','last_login_time','last_login_ip','host_group_id'
        ],'get');

        $this->validate($search,'ServerUser.index');
        $server = new ServerUserService();
        $list = $server->list($search);
        return $this->jsonOutPut($list);
    }

    /**
     * @name 添加
     * @author wx
     * @date 2019/10/28 15:23
     */
    public function add(Request $request)
    {
        //保存数据
        if($request->isPost()){
            $params = $request->only([
                'group_id','real_name','show_name','password','email','password_confirm','host_group_id'
            ],'post');

            $this->validate($params,'ServerUser.add');
            $server = new ServerUserService();
            $server->add($params);
            return $this->jsonOutPut();
        }
        //回显
        if($request->isGet()){
            //查询所有的资产组列表
            $hostGroupModel = new ServerHostGroupModel();
            $data['hostGroupList'] = $hostGroupModel->getAllList('id,name');
            return $this->jsonOutPut($data);
        }
    }

    /**
     * @name 编辑
     * @author wx
     * @date 2019/10/28 15:23
     */
    public function modify(Request $request)
    {
        //保存数据
        if($request->isPost()){
            //密码非必须
            $params = $request->only([
                'id','show_name','email' => '','host_group_id'
            ],'post');

            $this->validate($params,'ServerUser.modify');
            $server = new ServerUserService();
            $server->modify($params);
            return $this->jsonOutPut();
        }
        //回显
        if($request->isGet()){
            $params = $request->only([
                'id',
            ],'get');
            $this->validate($params,'ServerUser.modify_query');
            //查询所有的资产组列表
            $hostGroupModel = new ServerHostGroupModel();
            $data['hostGroupList'] = $hostGroupModel->getAllList('id,name');
            $userModel = new ServerUserModel();
            //原来该用户所属资产组id
            $params = $userModel->getInfoById($params['id'],'id,host_group_id');
            if(!$params){
                throw new CustomException('该用户已被删除,请刷新页面',450);
            }
            $data['host_group_id'] = $params['host_group_id'];
            return $this->jsonOutPut($data);
        }
    }

    /**
     * @name 删除
     * @author wx
     * @date 2019/10/28 15:23
     */
    public function del(Request $request)
    {
        $params = $request->only(['id'],'post');
        $this->validate($params,'ServerUser.del');
        $server = new ServerUserService();
        $server->del($params['id']);
        return $this->jsonOutPut();
    }

    /**
     * @name 修改当前登录用户密码
     * @author wx
     * @date 2019/10/28 15:23
     */
    public function modifyUserPassword(Request $request)
    {
        //接收参数
        $params = $request->only([
            'oldPassword','password','password_confirm'
        ],'post');

        $this->validate($params,'ServerUser.modifyUserPassword');
        $server = new ServerUserService();
        $server->modifyUserPassword($params['oldPassword'],$params['password']);
        return $this->jsonOutPut();
    }

    /**
     * 重置登录密码
    */
    public function resetPassword(Request $request){
        $param = $request->only([
            'id','password','password_confirm','login_user_password'
        ],'post');
        $this->validate($param,'ServerUser.resetPassword');
        $model = new ServerUserModel();
        $res = $model->resetPassword($param);
        return $this->jsonOutPut($res);
    }
}